﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using Lenovo.Smart.BLL.Dic;
using Lenovo.Smart.BLL.Sys;
using Lenovo.Smart.BLL.Mt;
using Lenovo.Smart.Common.Sys;
using Lenovo.Smart.Common.Cv;

namespace Lenovo.Smart.Web.Helper
{
    public class BindHelper
    {
        public static void SetSelectItem(ListControl ddl, string selectedValue)
        {
            if (ddl.SelectedItem != null)
                ddl.SelectedItem.Selected = false;
            ListItem item = ddl.Items.FindByValue(selectedValue);
            if (item != null)
                item.Selected = true;
        }

        private static void SetDefaultItem(ListControl ddl, string defaultText, string defaultValue, string selectedValue)
        {
            if (!String.IsNullOrEmpty(defaultText))
            {
                ddl.Items.Insert(0, new ListItem(defaultText, defaultValue));
            }

            if (!String.IsNullOrEmpty(selectedValue))
            {
                ListItem item = ddl.Items.FindByValue(selectedValue);
                if (item != null)
                    item.Selected = true;
            }
        }

        public static void BindProgram(ListControl ddl, string defaultText, string defaultValue, string selectedValue)
        {
            ddl.Items.Clear();

            ddl.DataSource = new ProgramBl().GetProgram();
            ddl.DataTextField = "Code";
            ddl.DataValueField = "ID";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

        public static void BindUser(ListControl ddl, string defaultText, string defaultValue, string selectedValue)
        {
            ddl.Items.Clear();

            ddl.DataSource = new UserBl().GetUserAll();
            ddl.DataTextField = "Username";
            ddl.DataValueField = "Username";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

        public static void BindGeography(ListControl ddl, string defaultText, string defaultValue, string selectedValue, bool withHidden)
        {
            ddl.Items.Clear();

            ddl.DataSource = GeographyDicMgr.Get(withHidden);
            ddl.DataTextField = "Title";
            ddl.DataValueField = "Code";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

        public static void BindRegion(ListControl ddl, string defaultText, string defaultValue, string selectedValue, bool withHidden)
        {
            ddl.Items.Clear();

            ddl.DataSource = RegionDicMgr.Get(withHidden);
            ddl.DataTextField = "Title";
            ddl.DataValueField = "Code";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

        public static void BindCountry(ListControl ddl, string defaultText, string defaultValue, string selectedValue, bool withHidden)
        {
            ddl.Items.Clear();

            ddl.DataSource = CountryDicMgr.Get(withHidden);
            ddl.DataTextField = "Title";
            ddl.DataValueField = "Code";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }
        
        public static void BindGeo4Warranty(ListControl ddl, string defaultText, string defaultValue, string selectedValue, bool withHidden)
        {
            ddl.Items.Clear();

            ddl.DataSource = Geo4WarrantyDicMgr.Get(withHidden);
            ddl.DataTextField = "Title";
            ddl.DataValueField = "Code";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

        public static void BindSeries(ListControl ddl, string defaultText, string defaultValue, string selectedValue, bool withHidden)
        {
            ddl.Items.Clear();

            ddl.DataSource = SeriesDicMgr.Get(withHidden);
            ddl.DataTextField = "Title";
            ddl.DataValueField = "Code";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

        public static void BindSubSeries(ListControl ddl, string defaultText, string defaultValue, string selectedValue, bool withHidden, string series)
        {
            ddl.Items.Clear();

            ddl.DataSource = SubSeriesDicMgr.Get(series, withHidden);
            ddl.DataTextField = "Title";
            ddl.DataValueField = "Code";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

        public static void BindTR(ListControl ddl, string defaultText, string defaultValue, string selectedValue, bool withHidden)
        {
            ddl.Items.Clear();

            ddl.DataSource = TRDicMgr.Get(withHidden);
            ddl.DataTextField = "Title";
            ddl.DataValueField = "Code";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

        public static void BindFactory(ListControl ddl, string defaultText, string defaultValue, string selectedValue, bool withHidden)
        {
            ddl.Items.Clear();

            ddl.DataSource = FactoryDicMgr.Get(withHidden);
            ddl.DataTextField = "Title";
            ddl.DataValueField = "Code";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

        public static void BindTeam(ListControl ddl, string defaultText, string defaultValue, string selectedValue, bool withHidden)
        {
            ddl.Items.Clear();

            ddl.DataSource = TeamDicMgr.Get(withHidden);
            ddl.DataTextField = "Title";
            ddl.DataValueField = "Code";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

        public static void BindWarrantyTeam(ListControl ddl, string defaultText, string defaultValue, string selectedValue, bool withHidden)
        {
            ddl.Items.Clear();

            ddl.DataSource = WarrantyDicMgr.Get(withHidden);
            ddl.DataTextField = "Title";
            ddl.DataValueField = "Code";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

        public static void BindGeoWarrantyCandidator(ListControl ddl, string defaultText, string defaultValue, string selectedValue)
        {
            ddl.Items.Clear();

            ddl.Items.Add(new ListItem("1/1 On-Site", "110"));
            ddl.Items.Add(new ListItem("3/1 On-Site", "310"));
            ddl.Items.Add(new ListItem("3/3 On-Site", "330"));
            ddl.Items.Add(new ListItem("4/4 On-Site", "440"));

            ddl.Items.Add(new ListItem("1/1 Carry-In", "111"));
            ddl.Items.Add(new ListItem("3/1 Carry-In", "311"));
            ddl.Items.Add(new ListItem("3/3 Carry-In", "331"));
            ddl.Items.Add(new ListItem("4/4 Carry-In", "441"));

            ddl.Items.Add(new ListItem("1/1 Courier", "112"));
            ddl.Items.Add(new ListItem("3/1 Courier", "312"));
            ddl.Items.Add(new ListItem("3/3 Courier", "332"));
            ddl.Items.Add(new ListItem("4/4 Courier", "442"));

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

        /// <summary>
        /// 显示Chassis
        /// </summary>
        /// <param name="ddl"></param>
        /// <param name="defaultText"></param>
        /// <param name="defaultValue"></param>
        /// <param name="selectedValue"></param>
        /// <param name="prog"></param>
        public static void BindMech(ListControl ddl, string defaultText, string defaultValue, string selectedValue, Program prog)
        {
            ddl.Items.Clear();

            ddl.DataSource = new MechanicalBl().GetMechanical(prog);
            ddl.DataTextField = "Chassis";
            ddl.DataValueField = "ID";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

        public static void BindMechAbbr(ListControl ddl, string defaultText, string defaultValue, string selectedValue, Program prog)
        {
            ddl.Items.Clear();

            ddl.DataSource = new MechanicalBl().GetMechanical(prog);
            ddl.DataTextField = "ChassisAbbr";
            ddl.DataValueField = "ID";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }


        public static void BindChara(ListControl ddl, string defaultText, string defaultValue, string selectedValue, List<CentralChara> charas)
        {
            ddl.Items.Clear();

            foreach(CentralChara chara in charas)
            {
                ddl.Items.Add(new ListItem(chara.Description, chara.ID.ToString()));
            }

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

        public static void BindValue(ListControl ddl, string defaultText, string defaultValue, string selectedValue, List<CentralValue> values)
        {
            ddl.Items.Clear();

            foreach (CentralValue chara in values)
            {
                ddl.Items.Add(new ListItem(chara.Description, chara.ID.ToString()));
            }

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }


        public static void BindSbbType(ListControl ddl, string defaultText, string defaultValue, string selectedValue, bool withHidden)
        {
            ddl.Items.Clear();

            ddl.DataSource = SbbTypeDicMgr.Get(withHidden);
            ddl.DataTextField = "Title";
            ddl.DataValueField = "Code";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

        public static void BindSbbTypeWithCode(ListControl ddl, string defaultText, string defaultValue, string selectedValue, bool withHidden)
        {
            ddl.Items.Clear();

            ddl.DataSource = SbbTypeDicMgr.Get(withHidden);
            ddl.DataTextField = "Code";
            ddl.DataValueField = "Code";
            ddl.DataBind();

            SetDefaultItem(ddl, defaultText, defaultValue, selectedValue);
        }

    }
}
